//
//  ViewPwdmanger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCPwdmanger: VCBase {
    
    private lazy var modifyPwdView:ModifyPwdView = {
        let ui = ModifyPwdView()
        ui.backgroundColor = UIColor.white
        let uiY = realHeight + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_views()
        init_event()
    }
}

// MARK: 代码逻辑
extension VCPwdmanger {
    
    func modifyPasword(bean:CallbackBean) -> Void {
        if bean.first?.count == 0 {
            self.show(text: "新密码不能为空")
            return
        }
        if bean.second?.count == 0 {
            self.show(text: "确认密码不能为空")
            return
        }
        if !bean.first!.elementsEqual(bean.second!) {
            self.show(text: "密码不一致，请重新输入")
            return
        }
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(userid, forKey: SP_USERID)
        dict.setValue(bean.first, forKey: SP_PASSWORD)
        // 请求
        self.doPost(api: "modify/password", dict: dict) { (dict) in
            self.show(text: "修改成功")
            UserDefaults.standard.setValue(bean.first, forKey: SP_PASSWORD)
            self.navigationController?.popViewController(animated: true)
        }
    }
}

// MARK: 初始化
extension VCPwdmanger {
    
    private func init_event() -> Void {
        modifyPwdView.complete = {bean in
            self.modifyPasword(bean: bean)
        }
    }
    
    private func init_views() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(modifyPwdView)
    }
}
